var WINLIST = require("../config/pagejson");
/**
 * win打开通用类
 * 属性isHead 0:无头部|1:自用头部|2:共用头部
 * 属性login 0代表无需登录，默认无需登录，1代表需要登录才能访问
 * title如果共用头部调用title属性
 * path打开页面路径
 */
module.exports = {
    Has : {},
    openWin: function(name, params) {
        var obj = this._checkPageUrl(name, params);
        if(obj.winUrl) {
            obj.params.openType = 'win';
            api.openWin({
                name : name,
                url : obj.winUrl,
                pageParam : obj.params
            });
        }
    },
    openFrame: function(name, params) {
        var obj = this._checkPageUrl(name, params);
        if(obj.winUrl) {
            obj.params.openType = 'frame';
            api.openFrame({
                name: name,
                url: obj.winUrl,
                rect: this._frameCommonRect,
                animation:this._frameCommonAnimation,
                pageParam: obj.params
            });
        }
    },
    //关闭页面
    closePage: function(name, openType) {
       if(openType == 'frame') {
           this.closeFrame(name);
       } else {
           this.closeWin(name);
       } 
    },
    //关闭页面
    closeWin : function(name, callback) {
        var params = {
            name : name
        };
        if (!name) {
            params = {};
        }
        api.closeWin(params);
        $.isFunction(callback) && (callback());
    },
    //frame隐藏
    frameGroupHide:function(name) {
        api.setFrameGroupAttr({
            name: name,
            hidden: true
        });
    },
    //frame组显示
    frameGroupShow:function(name) {
        api.setFrameGroupAttr({
            name: name,
            hidden: false
        });
    },
    closeFrame: function(name) {
        var param = param = {};
        if(name) {
            param.name = name;
        }
        api.closeFrame(param);
    },
    _frameCommonRect: function() {
        return {
            x:0,
            y:0,
            w:api.winWidth,
            h:api.winHeight
        };
    },
    _frameCommonAnimation : {
        type:"fade",       
        duration:100,
        subType:"from_bottom",
    },
    //根据名称获取
    _checkPageUrl: function(name, params) {
        if(!WINLIST.hasOwnProperty(name)) {
            API.errorToast("未定义页面");
            return false;
        }
        var names = name.split('_');
        if(names.length <2) {
            API.errorToast("错误传参方式");
            return false;
        }
        var winObj = WINLIST[name];
        var basePath = "../../";
        var _url = basePath+names[0]+'/'+names[1]+'/'+names[1]+'.html';
        var winUrl = _url;
        if(!params) {
            params = {};
        }
        switch(winObj.isHead) {
            //自建header页面
            case 1:
                winUrl = basePath+names[0]+'/'+names[1]+'/head.html';
                //console.log(winUrl);
            break;
            
            //共用header页面
            case 2:
                winUrl = basePath+'public/head/head.html';
                params.frameName = name + '_frame';
                params.frameUrl = _url;
                params.title = winObj.title;
            break;
            
            //默认无header页面
            default:
            break;
        }
        return {winUrl: winUrl, params : params};
    },
	// 加载动画
	isload: function(){
		$('.spinner_loading-outbox').remove();//移除以免重复加载
		var _hml = '<div class="spinner_loading-outbox isload">'+
						'<div class="spinner_loading-box">'+
							'<div class="spinner_loading">'+
								'<div class="spinner-container loading_container1">'+
									'<div class="circle1"></div>'+
									'<div class="circle2"></div>'+
									'<div class="circle3"></div>'+
									'<div class="circle4"></div>'+
								'</div>'+
								'<div class="spinner-container loading_container2">'+
									'<div class="circle1"></div>'+
									'<div class="circle2"></div>'+
									'<div class="circle3"></div>'+
									'<div class="circle4"></div>'+
								'</div>'+
								'<div class="spinner-container loading_container3">'+
									'<div class="circle1"></div>'+
									'<div class="circle2"></div>'+
									'<div class="circle3"></div>'+
									'<div class="circle4"></div>'+
								'</div>'+
							'</div>'+
							'<div class="text">数据正在加载中</div>'+
						'</div>'+
					'</div>';
		$('body').append(_hml);
	},
}
